﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class FormDeliver
    Dim objGJBck As New clsGJBck
    Public _sys_id As String
    Public usr As String
    Dim httpHelper As New HttpHelper
    Dim warehousecode As String
    Private Sub FormDeliver_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        warehousecode = GetIniFile("WAREHOUSE", "warehouse_code", "", g_szInitFileName)
        txtDL_no.Focus()
        'setEmptyLOC()
    End Sub

    Private Sub setEmptyLOC()
        Dim objMstList As New clsMstList
        Dim dt As DataTable
        loc.Items.Clear()
        dt = objMstList.getEmptyLoc("2")

        If dt.Rows.Count > 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1
                loc.Items.Add(dt.Rows(i)("mst_value"))
            Next
        End If
    End Sub

    Private Sub btnOUT_Click(sender As Object, e As EventArgs) Handles btnOUT.Click
        If String.IsNullOrEmpty(txtDL_no.Text.Trim()) Then
            MsgBox("请扫描或输入吊篮号")
            Exit Sub
        End If
        If String.IsNullOrEmpty(usr) = False Then
            If String.IsNullOrEmpty(_sys_id) = False Then
                If objGJBck.outCK(loc.Text, _sys_id, usr) = -2 Then
                    MsgBox("出库失败")
                Else
                    Dim rnt_int As Integer = mesCk(rk_num.Text, warehousecode, txt_material_code.Text.Trim())
                    clear()
                    If rnt_int = 1 Then
                        MsgBox("出库成功")
                    Else
                        MsgBox("出库成功,MES出库失败")
                    End If
                    txtDL_no.Text = String.Empty
                    txtDL_no.Focus()
                End If
            End If
        End If
    End Sub

    Private Sub txtDL_no_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtDL_no.KeyPress
        If String.IsNullOrEmpty(txtDL_no.Text.Trim()) = False Then
            If e.KeyChar = vbCr Then
                Dim dt As DataTable = objGJBck.getListByDlNo(txtDL_no.Text.Trim(), "0")
                If dt.Rows.Count = 1 Then
                    _sys_id = dt.Rows(0)("sys_id")
                    gjb_type.Text = dt.Rows(0)("gjb_type")
                    txt_material_code.Text = dt.Rows(0)("material_code")
                    rkdt.Text = dt.Rows(0)("begin_date")
                    rk_num.Text = dt.Rows(0)("into_num")
                    loc.SelectedText = dt.Rows(0)("location")
                Else
                    If dt.Rows.Count = 0 Then
                        MsgBox("未查询到骨架板吊篮库存信息")
                        txtDL_no.Text = String.Empty
                        txtDL_no.Focus()
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub txtDL_no_TextChanged(sender As Object, e As EventArgs) Handles txtDL_no.TextChanged
        clear()
    End Sub

    Public Sub clear()
        gjb_type.Text = String.Empty
        rkdt.Text = String.Empty
        txt_material_code.Text = String.Empty
        rk_num.Text = 0
        loc.Text = String.Empty
        _sys_id = String.Empty
    End Sub

    Private Function mesCk(oper_num As String, warehousecode As String, productorname As String) As Integer
        Dim rtn_int As Integer = 0
        Dim params As String = "{"
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """xhcount"":""" & Double.Parse(oper_num) & """," '出库数量
        params &= """warehousecode"":""" & warehousecode & """," '出库仓库编码
        params &= """productorname"":""" & productorname & """" '产品编码
        params &= "}"
        Dim rtn As String = HttpHelper.HttpPostWebService("materialsConsumption", params)
        Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
        Dim code As String = jo("code").ToString() '输出0
        Dim msg As String = jo("msg").ToString() '输出操作成功
        If code.Equals("0") And msg.Equals("操作成功") Then
            rtn_int = 1
        End If
        Return rtn_int
    End Function

End Class